/*
This file is part of IRCaBot.
IRCaBot is IRC logger with features.
Source code: https://notabug.org/acetone/ircabot.
Copyright (C) acetone, 2023.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "config.h"
#include "libirc/irc.h"
#include "voicedlist.h"

#include <QMap>
#include <QMutex>
#include <QObject>

class IRCNetworkClient : public QObject
{
    friend IRCNetworkClientStarter;
    Q_OBJECT
public:
    IRCNetworkClient(QObject* parent = nullptr);
    ~IRCNetworkClient();
    void setConfig(QSharedPointer<Config::Network> config);
    // after config change; voiceGate have effect in run-time automatically
    uint reloadChannelList();
    void addNicknameToVoicedGroup(const QString& nickname);

    const QSharedPointer<libircclient::Network> network() { return m_network; }

signals:
    void Event_NewDirectMessage(QString sender, QString text);
    void Event_NewChannelMessage(QString channel, QString sender, QString text);
    void stopped();
    void doStopPrivate();

public slots:
    void start();
    void stop();

private slots:
    void Slot_privMsg(libircclient::Parser* p);
    void Slot_adminIdentified(libircclient::Parser* p);
    void Slot_onDirectMessage(QString sender, QString text);
    void Slot_onChannelMessage(QString channel, QString sender, QString text);
    void Slot_onJoin(libircclient::Parser*, libircclient::User* u, libircclient::Channel* c);
    void Slot_onMode(libircclient::Parser* p);
    void Slot_onWelcome();
    void Slot_onDisconnected();
    void stopPrivate();

private:
    void processAdminCommand(const QString& text);

    void processVoiceRequest(const QString& sender);
    void sendVoiceGateInvite(const QString& nickname);
    QList<libircclient::Channel *> getChannelsWhereIAmHavePrivileges();
    void giveTheVoiceFlag(const QStringList& nicknames, const QString& channel = "");
    void giveTheVoiceFlagIfItsWillBeOkOrSendInvite(const QString& nickname, libircclient::Channel* channel);

    QSharedPointer<Config::Network> m_config = nullptr;
    QSharedPointer<libircclient::Network> m_network = nullptr;

    QString m_adminCommandBuffer;

    QMap<QString, qint64> m_voiceGateRequestsTimestamp; // nickname
    QMap<QString, qint64> m_voiceGateInviteSendedTimestamp; // nickname
    VoicedList m_voicedList;
    QMutex m_mtxNetworkChannelsIteration;
    QMutex m_mtxvoiceGateRequestsTimestamp;

    uint8_t m_sleepPeriod = 1; // reconnect timer
};

